(function() {

var OVERLAY_CLASS = 'socialdashboard-overlay';

function init() {
  var elements = document.body.childNodes;
  var initialized = false;
  for (var i=0, len=elements.length; i<len; i++) {
    var el = elements[i];
    if (el.className == OVERLAY_CLASS) {
      initialized = true;
      el.style.display = el.style.display=='none' ? 'block' : 'none';
    }
  }
  if (!initialized) setup();
}

function setup() {
  var env = {};
  env.locationUrl = window.location.href;
  env.selectedText = 
    window.getSelection ? window.getSelection().toString() :
    document.getSelection ? document.getSelection() :
    document.selection ?  document.selection.createRange().text :
    '';

  var iframe = overlay();
  iframe.contentWindow.name = toJsonLite(env);
  iframe.contentWindow.location.href = getPage() + '#overlay';
}

function toJsonLite(obj) {
  var pairs = [];
  for (var key in obj) {
    pairs.push('"'+esc(key)+'":"'+esc(obj[key])+'"');
  }
  return '{'+pairs.join(',')+'}';
}

function esc(str) {
  return str && str.replace(/"/g,'\\"').replace(/\n/g, '\\n').replace(/\r/g, '\\r');
}

function overlay() {
  var isIE = !!window.ActiveXObject;
  var isIE6 = isIE && typeof document.body.style.maxHeight == "undefined";
  var isCSS1Compat = document.compatMode && document.compatMode == 'CSS1Compat';

  var shim = document.createElement('iframe');
  var div = document.createElement('div');
  var iframe = document.createElement('iframe');
  iframe.className = div.className = shim.className = OVERLAY_CLASS;
  iframe.frameBorder='0';
  iframe.allowTransparency=true;

  iframe.style.position = div.style.position = shim.style.position = isIE ? 'absolute' : 'fixed';

  iframe.style.top = div.style.top = shim.style.top = 
    iframe.style.left = div.style.left = shim.style.left = '0px';

  iframe.style.width = div.style.width = shim.style.width = isIE ? 
    Math.max(isCSS1Compat ? document.documentElement.clientWidth : document.body.clientWidth,
             isCSS1Compat ? document.documentElement.scrollWidth : document.body.scrollWidth)+'px' :
    '100%';

  iframe.style.height = isIE ?  
    (isCSS1Compat ? document.documentElement.clientHeight : document.body.clientHeight)+'px' :
    '100%';

  div.style.height = shim.style.height = isIE ? 
    Math.max(isCSS1Compat ? document.documentElement.clientHeight : document.body.clientHeight,
             isCSS1Compat ? document.documentElement.scrollHeight : document.body.scrollHeight)+'px' :
    '100%';
  setOpacity(shim, 0);
  setOpacity(div, .5);
  setOpacity(iframe, 0);

  div.style.backgroundColor = '#000';

  shim.style.zIndex = 99997;
  div.style.zIndex = 99998;
  iframe.style.zIndex = 99999;

  document.body.appendChild(shim);
  document.body.appendChild(div);
  document.body.appendChild(iframe);

  iframe.onload = function() {
    setOpacity(iframe, 1);
  }
  iframe.onreadystatechange = function() {
    if (iframe.readyState=='loaded' || iframe.readyState=='complete') {
      iframe.onload();
      iframe.onload = null;
    }
  }
  return iframe;
}

function getPage() {
  var scripts = document.getElementsByTagName('script');
  var src = scripts[scripts.length-1].src;
  var baseUrl = src.split('/').slice(0, -2).join('/');
  return baseUrl + '/index.html';
}

function setOpacity(el, opacity) {
  el.style.opacity = opacity;
  el.style.filter = 'alpha(opacity='+opacity+')';
}

init()

})()
